﻿package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import demo.app.model.vo.PolyShapeVO;
	import demo.app.view.ShapeEnvironment;
	import demo.utils.FrameRateCounter;
	
	import flash.desktop.NativeApplication;
	import flash.display.Graphics;
	import flash.display.Screen;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.AccelerometerEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.sensors.Accelerometer;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.text.TextFormat;

	//import flash.desktop.NativeApplication;
	//import flash.desktop.SystemIdleMode;
	[SWF(frameRate="30")]
	public class Box2D extends Sprite
	{		
		private var _colors:Array = new Array(0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF);
		private var _shape2d:demo.app.view.ShapeEnvironment;
		private var _output:TextField;
		private var _accelerometer:Accelerometer;
		private var m_GravityShape:Shape = new Shape();
		private var _hasAccel:Boolean = false;
		
		
		public function Box2D()
		{		
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			if(Capabilities.cpuArchitecture=="ARM")
			{
				NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
				NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
			}
			
			var background:Sprite = new Sprite();
			background.graphics.lineStyle(20, 0x000000);
			background.graphics.drawRect(0,0,Capabilities.screenResolutionX,Capabilities.screenResolutionY);
			this.addChild(background);
			
			_shape2d = new ShapeEnvironment(Capabilities.screenResolutionX - 20,Capabilities.screenResolutionY - 20, false);
			_shape2d.x = 10;
			_shape2d.y = 10;
			
			_shape2d.start();
			
			this.addChild(_shape2d);
			
			//stage.addEventListener(Event.RESIZE, onChanged, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, addCounterGravity, false, 0, true);
			
			var framerateCounter:FrameRateCounter = new FrameRateCounter();
			framerateCounter.x = 10;
			framerateCounter.y = 10;
			addChild(framerateCounter);
			this.addChild(m_GravityShape);
			this.addEventListener(Event.ENTER_FRAME, mouseMove);
			//_shape2d.gravityX = 0;
			//_shape2d.gravityY = 10;
			if(Accelerometer.isSupported){
				_accelerometer = new Accelerometer();
				_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onChanged);
			}
			
			

			//_accelerometer.setRequestedUpdateInterval(1000);
			
		}
		
		private function handleDeactivate(event:Event):void
		{
			NativeApplication.nativeApplication.exit();
		}
		
		private function handleKeys(event:KeyboardEvent):void
		{
			if(event.keyCode == 16777238){
				NativeApplication.nativeApplication.exit();
			}
		}
		
		private function mouseMove(e:Event):void
		{				
			if(!_hasAccel){
				if(this.mouseX > 0 && this.mouseX < stage.stageWidth){
					_shape2d.gravityX = (this.mouseX - (stage.stageWidth /2)) / 40;
				}
				if(this.mouseY > 0 && this.mouseY < stage.stageHeight){
					_shape2d.gravityY = (this.mouseY - (stage.stageHeight /2)) / 40;
				}
				m_GravityShape.graphics.clear();
				m_GravityShape.graphics.lineStyle(4, 0x000000);
				m_GravityShape.graphics.moveTo((stage.stageWidth /2), (stage.stageHeight /2));
				m_GravityShape.graphics.lineTo(this.mouseX, this.mouseY);
			}
		}
		
		private function onChanged(evt:AccelerometerEvent):void{
			if(!_hasAccel){
				this.removeEventListener(Event.ENTER_FRAME, mouseMove);
				this.removeChild(m_GravityShape);
				_hasAccel = true;
			}
			_shape2d.gravityX = -evt.accelerationX * 50;
			_shape2d.gravityY = evt.accelerationY * 50;
		}
		
		protected function addCounterGravity(event:MouseEvent):void
		{
			//_shape2d.counterGravity = true;
			var tempShapeVO:PolyShapeVO = new PolyShapeVO();
			tempShapeVO.color = _colors[0];
			tempShapeVO.numSides = 4;
			tempShapeVO.radius = 36;
			tempShapeVO.xPos = this.mouseX;
			tempShapeVO.yPos = this.mouseY;
			_shape2d.addPoly(tempShapeVO);
		}
	}
}